﻿/***************************************************************************
//* Foxit Software Inc.
//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
//* and trade secrets of Foxit Software Inc.                                                      *
***************************************************************************/

/**
* 缩略图显示
*/

define(function (require, exports, module) {
    var CPDF_PageThumb = require('./PDFThumb');
    FXREADER.PDFView.CPDF_PageThumbsView = function (pPDFDoc, MainFrameID, pReaderApp) {
        if (pPDFDoc == null)
            throw new Error("pPDFDoc is null!");

        var _ThumbList = {},              //缩略图列表;
             _ThumbsViewContainerID = MainFrameID + "_ThumbsViewContainer",
             _MainFrameID = MainFrameID,
             _pReaderApp = pReaderApp,
        //视口宽度;
            _ThumbsViewWidth = 0,
        //视口高度;
             _ThumbsViewHeight = 0,
        //用于步进式页面创建，存储已经创建的页面列表，数组中每个对象为CPageInterval类型
              _CreatedPageArray = [],
        //处于显示区域的页面;
               _VisablePageRange = { begin: 0, end: 0, current: 0 },
        //滚动条api;
               _ScrollApi = null,
        //当前页面索引;
               _iCurPageIndex = 0,
        //当前用于焦点的页面;
                _CurSelectedPage = -1,
        //当前处于选中状态的页面;
               _nThumbFocusPage = -1,
               _pPDFDoc = pPDFDoc,
               _self = this;
        var _BlankImg = "";
        var _ThumbImgLeftOffset = FXREADER.PDFView.STANDARD_THUMB_CONTAINER_WIDTH - FXREADER.PDFView.STANDARD_THUMB_WIDTH,
             _ThumbImgTopOffset = FXREADER.PDFView.STANDARD_THUMB_CONTAINER_HEIGHT - FXREADER.PDFView.STANDARD_THUMB_HEIGHT;
        var _documentScrollTimeout = null;
        //检查对应的页面是否已经创建;
        function _CheckPageCreated(nPageIndex) {
            for (var i = 0; i < _CreatedPageArray.length; i++) {
                var interval = _CreatedPageArray[i];
                if (interval.m_nStartIndex <= nPageIndex && interval.m_nEndIndex >= nPageIndex)
                    return true;
            }
            return false;
        }

        //根据页面索引，获取当前已经创建的，并且在对应页面之前，同时最接近对应页面的页面索引;
        function _GetInsertAfterIndex(nPageIndex) {
            var nInsertIndex = -1;
            var nMinDistance = nPageIndex;
            for (var i = 0; i < _CreatedPageArray.length; i++) {
                var interval = _CreatedPageArray[i];
                var tmpDistance = nPageIndex - interval.m_nEndIndex;
                if (tmpDistance > 0 && tmpDistance < nMinDistance) {
                    nMinDistance = tmpDistance;
                    nInsertIndex = interval.m_nEndIndex;
                }
            }
            return nInsertIndex;
        }


        /* 根据页面索引，获取需要创建的页面区间;*/
        function _GetPageInterval2Create(nPageIndex) {
            var maxPage2Create = FXREADER.Config.maxNewPagesPerTime;
            //为-1 表示创建所有页面;
            if (maxPage2Create == -1)
                maxPage2Create = _self.GetPageCount();
            var iStartIndex = parseInt(nPageIndex / maxPage2Create) * maxPage2Create;
            return new FXREADER.PDFView.CPageInterval(iStartIndex, iStartIndex + maxPage2Create - 1);
        }
        //判断页面是否处于显示区域;
        function _IsPageInVisableRange(iPageIndex) {
            return iPageIndex >= _VisablePageRange.begin && iPageIndex <= _VisablePageRange.end;
        }

        //更新页面索引;
        function _UpdatePageIndex() {
            var pPageView = _self.GetThumb(_iCurPageIndex);
            //通过App修改工具栏上的页面索引
        }

        //public methods
        this.GetThumbsViewContainerID = function () {
            return _ThumbsViewContainerID;
        };
        //根据页面索引，生成对应页面的Container ID;
        this.GetThumbContainerID = function (nPageIndex) {
            return _ThumbsViewContainerID + "_ThumbContainer_" + nPageIndex.toString();
        };
        //根据页面索引，生成对应页面背景图ID;
        this.GetThumbBGImgID = function (nPageIndex) {
            return _ThumbsViewContainerID + "_BG_" + nPageIndex.toString();
        };
        //根据页面索引，生成对应页面Wrap ID;
        this.GetWrapThumbID = function (nPageIndex) {
            return _ThumbsViewContainerID + "_Wrap_" + nPageIndex.toString();
        };
        //根据页面索引，生成对应页面Loading 图标 ID;
        this.GetThumbLoaderImgID = function (nPageIndex) {
            return _ThumbsViewContainerID + "_Loader_" + nPageIndex.toString();
        };

        this.GetThumbNumberID = function (nPageIndex) {
            return _ThumbsViewContainerID + "_PageNum_" + nPageIndex.toString();
        };

        this.SetThumbsViewWidth = function (width) {
            _ThumbsViewWidth = width;
        };
        this.GetThumbsViewWidth = function () {
            return _ThumbsViewWidth;
        };
        this.SetThumbsViewHeight = function (height) {
            _ThumbsViewHeight = height;
        };
        this.GetThumbsViewHeight = function () {
            return _ThumbsViewHeight;
        };

        //获取页数
        this.GetPageCount = function () {
            return _pPDFDoc.GetPageCount();
        };

        this.GetThumb = function (nPageIndex) {
            return _ThumbList[nPageIndex];
        };


        //初始化DocView;
        this.Init = function () {
            var iPageCnt = _pPDFDoc.GetPageCount();

            for (var i = 0; i < iPageCnt; i++) {
                _ThumbList[i] = new CPDF_PageThumb(_pPDFDoc.GetPage(i), _self, _ThumbsViewContainerID);
            }
            var thumbsViewHtml = _self.CreateThumbsViewHtml();
            var jqMainFrameObj = $("#" + _MainFrameID);
            jqMainFrameObj.append(thumbsViewHtml);
            _self.CreatePages(0);
            _self.InitializeScrollbar();
            _self.BindScrollEvent();
            _self.OnPageScroll();
        };

        this.BindScrollEvent = function () {
            $('#' + _ThumbsViewContainerID).scroll(_self.OnPageScroll);
        };

        this.UnBindScrollEvent = function () {
            $('#' + _ThumbsViewContainerID).unbind("scroll");
        };

        //获取已经创建的页面列表
        this.GetCreatedPageNums = function () {
            var k = 0;
            var pagenums = [];
            var iPageCnt = _self.GetPageCount();
            for (var i = 0; i < iPageCnt; i++) {
                if (_self.GetThumb(i).IsContentCreated()) {
                    pagenums[k] = i;
                    k++;
                }

            }
            return pagenums;
        };

        //创建页面区间中页面的div，并添加到dom;
        this.CreateThumbsView = function (nStartIndex, nEndIndex, nInsertAfterPageIndex) {
            var fragment = document.createDocumentFragment();
            var totalHtml = "";
            for (var i = nStartIndex; i <= nEndIndex; i++) {
                var pageHtml = _self.CreateThumbHtml(i);
                totalHtml += pageHtml;
                _self.GetThumb(i).SetContentCreatedState(true);
            }

            var insertPageID = _self.GetThumbContainerID(nInsertAfterPageIndex);
            var oElement = null;

            if (nInsertAfterPageIndex == -1)
                oElement = $("#" + _self.GetThumbsViewContainerID()).append(totalHtml);
            else {
                $(totalHtml).insertAfter($("#" + insertPageID));
            }

            return true;

        };

        //创建页面的div层;
        this.CreateThumbHtml = function (iPageIndex) {
            var pThumbView = _self.GetThumb(iPageIndex);
            if (pThumbView == null)
                return "";
            var pPDFPage = pThumbView.GetPDFPage();
            var fZoomX = parseFloat(pPDFPage.GetPageWidth() / FXREADER.PDFView.STANDARD_THUMB_WIDTH);
            var fZoomY = parseFloat(pPDFPage.GetPageHeight() / FXREADER.PDFView.STANDARD_THUMB_HEIGHT);
            pThumbView.SetThumbWidth(FXREADER.PDFView.STANDARD_THUMB_WIDTH);
            pThumbView.SetThumbHeight(FXREADER.PDFView.STANDARD_THUMB_HEIGHT);
            var fZoom = fZoomX > fZoomY ? fZoomX : fZoomY;
            var w, h, l, t;
            w = parseFloat(pPDFPage.GetPageWidth() / fZoom);
            h = parseFloat(pPDFPage.GetPageHeight() / fZoom);
            l = -w / 2;
            t = -h / 2;
            var loadingGif = "./images/loading.gif";
            var dimensions = "height:" + h + "px;width:" + w + "px;" + "left:50%;position:absolute;margin-left:" + l + "px;top:50%;margin-top:" + (t - 10) + "px;";
            var html = "<div id='" + _self.GetThumbContainerID(iPageIndex) + "' class='fwr_display_inline_block fwr_thumb_page' style='position:relative;display:inline-block;margin-left:10px;height:" +
                            FXREADER.PDFView.STANDARD_THUMB_CONTAINER_HEIGHT + "px;width:" + FXREADER.PDFView.STANDARD_THUMB_CONTAINER_WIDTH + "px;'>";
            html += "<div id='" + _self.GetWrapThumbID(iPageIndex)
                   + "' class='thumbimage border ' style='cursor: pointer;" + dimensions + "' page-index='" + iPageIndex + "'></div>"
                + "<img alt='' src='" + _BlankImg + "' id='" + _self.GetThumbBGImgID(iPageIndex)
                        + "' class='border  fwr_hidden fwr_thumb_image thumbimage' style='" + dimensions + ";background-size:cover;' page-index='" + iPageIndex + "'/>"
                        + "<img id='" + _self.GetThumbLoaderImgID(iPageIndex) + "' src='" + loadingGif
                        + "' style='position:absolute;left:50%;top:50%;margin-top: -12px;margin-left:-12px" + "' />";
            html += "<div class='fwr_page_number' style='width:20px;position:absolute;bottom:1px;left:50%;text-align:center;margin-left:-10px'><span class='' id='"
                + _self.GetThumbNumberID(iPageIndex) + "'>" + (iPageIndex + 1) + "</span></div>";
            html += "</div>";
            var fOffsetTop = (FXREADER.PDFView.STANDARD_THUMB_CONTAINER_HEIGHT - h) / 2 - 10;
            pThumbView.SetThumbImgOffsetTop(fOffsetTop);

            return html;
        };


        //创建包含所有页面的div层;
        this.CreateThumbsViewHtml = function () {
            var html = "<div id='" + _self.GetThumbsViewContainerID() + "' class='fwr_pages' style='height:100%;width:100%;overflow-y: hidden;overflow-x: hidden;"
                            + "'></div>";
            return html;
        };


        //根据页面索引，创建包含该页面的页面区间;
        this.CreatePages = function (nPageIndex) {
            var iPageCnt = _self.GetPageCount();
            if (nPageIndex < 0 || nPageIndex >= iPageCnt)
                return false;
            //已经创建;
            if (_CheckPageCreated(nPageIndex))
                return true;
            var pageInterval2Create = _GetPageInterval2Create(nPageIndex);
            if (pageInterval2Create.m_nStartIndex < 0)
                pageInterval2Create.m_nStartIndex = 0;
            if (pageInterval2Create.m_nEndIndex >= iPageCnt)
                pageInterval2Create.m_nEndIndex = iPageCnt - 1;
            var nInsertAfterPageIndex = _GetInsertAfterIndex(pageInterval2Create.m_nStartIndex);
            _CreatedPageArray.push(pageInterval2Create);
            return _self.CreateThumbsView(pageInterval2Create.m_nStartIndex, pageInterval2Create.m_nEndIndex, nInsertAfterPageIndex);
        };

        //初始化滚动条;
        this.InitializeScrollbar = function () {
            if (_ScrollApi != null)
                return _ScrollApi;
            var jqThumbsViewContainer = $("#" + _ThumbsViewContainerID);
            var api_element = jqThumbsViewContainer.jScrollPane({ showArrows: false, animateScroll: false,initTouchScreen:true,disableHorizontalScroll:true });
            _ScrollApi = api_element.data('jsp');
            return _ScrollApi;
        };
        //重新初始化滚动条;
        this.ReInitializeScrollBar = function () {
            if (_ScrollApi == null)
                throw new Error('Scroll Api is null!');
            _ScrollApi.reinitialise({ showArrows: false, animateScroll: false });
        };
        this.GetScrollApi = function () {
            return _ScrollApi;
        };


        this.GetDisplayPageRange = function () {
            return _VisablePageRange;
        };
        //获取处于当前显示区域的页面范围
        this.CountDisplayPageRange = function () {
            var range = {};
            range.begin = 0;
            range.end = 1;
            range.current = 0;
            var pagenums = _self.GetCreatedPageNums();
            var initedPageCnt = pagenums.length;
            var left = 0;
            var right = initedPageCnt - 1;
            var mid = (left + right) >> 1;
            var jqPagesContainerDiv = $("#" + _self.GetThumbsViewContainerID());
            var contentPaneHeight = jqPagesContainerDiv.height();
            var contentPanelWidth = jqPagesContainerDiv.width();
            var contentPanelOffset = jqPagesContainerDiv.offset();


            var conpos = jqPagesContainerDiv.position();
            while (mid > left) {
                var pPageView = _self.GetThumb(pagenums[mid]);
                var jqPage = $("#" + pPageView.GetThumbContainerID());
                var pos = jqPage.offset();
                var height = pPageView.GetThumbHeight();
                var width = pPageView.GetThumbWidth();
                var page_top = pos.top + pPageView.GetThumbImgOffsetTop();
                if ((page_top <= contentPanelOffset.top) && (page_top + height <= contentPanelOffset.top)) {
                    left = mid + 1;
                    mid = (left + right) >> 1;
                } else if ((page_top > (contentPanelOffset.top + contentPaneHeight))) {
                    right = mid - 1;
                    mid = (left + right) >> 1;
                } else {
                    break;
                }
            }

            range.begin = pagenums[mid];
            range.end = range.begin;

            for (var i = mid; i >= 0; i--) {
                var pPageView = _self.GetThumb(pagenums[i]);
                var jqPage = $("#" + pPageView.GetThumbContainerID());
                var pos = jqPage.offset();
                var height = pPageView.GetThumbHeight();
                var width = pPageView.GetThumbWidth();
                var pos2 = $("#" + pPageView.GetPageBGImgID()).offset();
                var page_top = pos.top + pPageView.GetThumbImgOffsetTop();
                if (page_top + height > contentPanelOffset.top) {
                    range.begin = pagenums[i];
                } else {
                    //mid页面不可见
                    if (i == mid && mid < initedPageCnt - 1) {
                        mid += 1;
                        range.begin = pagenums[mid];
                    }
                    break;
                }
            }

            range.current = range.begin;
            range.end = pagenums[mid];
            for (var i = mid + 1; i < initedPageCnt; i++) {
                var pPageView = _self.GetThumb(pagenums[i]);
                var jqPage = $("#" + pPageView.GetThumbContainerID());
                var pos = jqPage.offset();
                var height = pPageView.GetThumbHeight();
                var width = pPageView.GetThumbWidth();
                var page_top = pos.top + pPageView.GetThumbImgOffsetTop();
                if (page_top < contentPaneHeight + contentPanelOffset.top) {
                    range.end = pagenums[i];
                } else {
                    break;
                }
            }
            for (var i = range.begin; i <= range.end; i++) {
                var pPageView = _self.GetThumb(i);
                if (!pPageView.IsContentCreated()) {
                    range.begin = i + 1;
                    continue;
                }
            }


            for (var i = range.begin; i <= range.end; i++) {
                var pPageView = _self.GetThumb(i);
                if (!pPageView.IsContentCreated()) {
                    continue;
                }
                var jqPage = $("#" + pPageView.GetThumbContainerID());
                var height = pPageView.GetThumbHeight();
                var width = pPageView.GetThumbWidth();
                var pos = jqPage.offset();
                var page_top = pos.top + pPageView.GetThumbImgOffsetTop();
                if (((page_top >= contentPanelOffset.top) && (page_top < contentPanelOffset.top + contentPaneHeight)
                                                    && (page_top + height / 3 <= contentPanelOffset.top + contentPaneHeight))
                                                || ((page_top <= contentPanelOffset.top) && (page_top + height >= contentPanelOffset.top + contentPaneHeight * 2 / 3))) {
                    range.current = i;
                    break;
                }
                else {
                    continue;
                }

            }

            _VisablePageRange = range;
            return range;
        };

        //获取页面相对于视口区域的偏移
        this.GetThumbOffsetDocY = function (iThumbIndex) {
            var pThumbView = _self.GetThumb(iThumbIndex);
            if (!pThumbView.IsContentCreated())
                return 0;
            var thumbOffset = $("#" + pThumbView.GetThumbContainerID()).offset();
            var thumbsViewOffset = $("#" + _self.GetThumbsViewContainerID()).offset();
            return thumbsViewOffset.top - thumbOffset.top;
        };


        /**
        *步进式修改页面状态.;
        * @param [nPageIndex] 步进式处理后，要跳转到的页面索引;
        * @param [nStartPage] 要修改状态的页面起始索引;
        * @param [nEndPage] 要修改状态的页面结束索引;
        * @param [nOffsetY] 第一个参数指定页面相对于当前显示区域的y轴偏移量;
        * @param [bScrollX] 是否滚动X轴;
        * @param [bResizeScroll] 是否重新计算滚动条;
        */
        this.ProgressiveUpdateState = function (nPageIndex, nStartPage, nEndPage, nOffsetY, bScrollX, bResizeScroll) {
            var nSVisableIndex = 0, nEVisableIndex = 0;
            var bChange = false;
            var nd = FXREADER.Config.maxResizePagesPerTime;
            //为-1代表所有页面;
            if (nd == -1)
                nd = _self.GetPageCount();
            nSVisableIndex = nStartPage - nd > 0 ? nStartPage - nd : 0;
            nEVisableIndex = nEndPage + nd;
            if (nEVisableIndex >= _self.GetPageCount())
                nEVisableIndex = _self.GetPageCount() - 1;
            var nRelationPage = 0;
            var viewerContainerWidth = _self.GetThumbsViewWidth();
            for (var i = nSVisableIndex; i <= nEVisableIndex; i++) {
                var pPageView = _self.GetThumb(i);
                if (!pPageView.IsContentCreated())
                    continue;
                if (!pPageView.IsScaleUpdated()) {
                    bChange = true;
                }
                else
                    continue;
                pPageView.SetScaleUpdated(true);
                //pPageView.UpdateDimension(), pPageView.SetScaleUpdated(true);
                // pPageView.SetScale(_self.GetScale());
            }

            if (bChange && bResizeScroll) {
                if (_ScrollApi != null) {
                    _self.ReInitializeScrollBar();
                    var offsetX = _ScrollApi.getContentPositionX();
                    var pPageView = _self.GetThumb(nPageIndex);
                    _ScrollApi.scrollToElement("#" + pPageView.GetThumbContainerID(), true, false, nOffsetY);
                    bScrollX && _ScrollApi.scrollToX(offsetX);
                }
                else {
                    _self.InitializeScrollbar();
                }
                return true;
            }
            else
                return false;
        };


        //创建相邻页面;
        this.CreateNearPage = function (iDestPageIndex, offsetY) {
            var range = _self.CountDisplayPageRange();
            var nLastVisablePageIndex = range.end;
            var nFirstVisablePageIndex = range.begin;
            if (_ScrollApi != null) {
                var bUpdate = false;
                var nOldIndex = _iCurPageIndex;

                if (nFirstVisablePageIndex >= 1 && !_self.GetThumb(nFirstVisablePageIndex - 1).IsContentCreated()) {
                    bUpdate = _self.CreatePages(nFirstVisablePageIndex - 1);
                }
                if (nLastVisablePageIndex < _self.GetPageCount() - 1 && !_self.GetThumb(nLastVisablePageIndex + 1).IsContentCreated()) {
                    bUpdate |= _self.CreatePages(nLastVisablePageIndex + 1);
                }

                if (bUpdate) {
                    var offsetX = _ScrollApi.getContentPositionX();
                    range = _self.CountDisplayPageRange();
                    nLastVisablePageIndex = range.end;
                    nFirstVisablePageIndex = range.begin;
                    _self.ProgressiveUpdateState(iDestPageIndex, nFirstVisablePageIndex, nLastVisablePageIndex, offsetY, true, false);
                    //继续创建直到超出当前可视区域;
                    _self.ReInitializeScrollBar();
                    _ScrollApi.scrollToElement("#" + _self.GetThumb(iDestPageIndex).GetThumbContainerID(), true, false, offsetY);
                    _ScrollApi.scrollToX(offsetX);
                    _self.CreateNearPage(iDestPageIndex, offsetY);
                    _self.ReInitializeScrollBar();
                    _ScrollApi.scrollToElement("#" + _self.GetThumb(iDestPageIndex).GetThumbContainerID(), true, false, offsetY);
                    _ScrollApi.scrollToX(offsetX);
                }
            }
        };

        this.OnPageScroll = function () {
            var oldVisableRange = _self.GetDisplayPageRange();
            var range = _self.CountDisplayPageRange();
            var nLastVisablePageIndex = range.end;
            var nFirstVisablePageIndex = range.begin;
            if (_ScrollApi != null) {
                var offsetX = _ScrollApi.getContentPositionX();
                var bUpdate = false;
                var nOldIndex = _iCurPageIndex;
                var offsetY = _self.GetThumbOffsetDocY(_iCurPageIndex);

                if (nFirstVisablePageIndex >= 1 && !_self.GetThumb(nFirstVisablePageIndex - 1).IsContentCreated()) {
                    bUpdate = _self.CreatePages(nFirstVisablePageIndex - 1);
                }
                if (nLastVisablePageIndex < _self.GetPageCount() - 1 && !_self.GetThumb(nLastVisablePageIndex + 1).IsContentCreated()) {
                    bUpdate |= _self.CreatePages(nLastVisablePageIndex + 1);
                }
                if (bUpdate) {
                    _ScrollApi.scrollToX(offsetX);
                }
                _self.ProgressiveUpdateState(_iCurPageIndex, nFirstVisablePageIndex, nLastVisablePageIndex, offsetY, true, true);
            }
            range = _self.CountDisplayPageRange();
            _iCurPageIndex = range.current;
            _UpdatePageIndex();
            // Resets timeout to prevent page load per scroll event
            if (_documentScrollTimeout) {
                clearTimeout(_documentScrollTimeout);
            }
            _documentScrollTimeout = setTimeout(function () {
                _self.LoadPages(range);
            }, 200);

        };

        //加载页面;
        this.LoadPages = function (range) {
            for (var i = range.begin; i <= range.end; i++) {
                var pThumb = _self.GetThumb(i);
                _pReaderApp.GetMainView().GetDocView().GetPageViewRender().RenderPage(pThumb,FXREADER.Config.requestRetryCount);
            }
        };

        /**
        * 跳转到对应页面.;
        * @param [iPageIndex] 页面索引;
        * @param [OffsetX] 相对要跳转页面的左边距离;
        * @param [OffsetY] 相对要跳转页面的顶部距离;
        */
        this.GotoPage = function (iPageIndex, OffsetX, OffsetY) {
            if (_ScrollApi == null)
                return;
            var oldVisableRange = _self.GetDisplayPageRange();
            //先取消监控滚动事件;
            _self.UnBindScrollEvent();
            OffsetY = OffsetY ? OffsetY : 0;
            _nDestPageOffsetY = OffsetY;
            if (OffsetX == null)
                OffsetX = _ScrollApi.getContentPositionX();
            if (iPageIndex < 0 || iPageIndex >= _self.GetPageCount())
                return;
            var bUpdate = false;
            var nMaxNewPagesPerTime = FXREADER.Config.maxNewPagesPerTime;
            if (nMaxNewPagesPerTime == -1)
                nMaxNewPagesPerTime = _self.GetPageCount();
            if ((iPageIndex + 1) % nMaxNewPagesPerTime == 0) {
                bUpdate = _self.CreatePages(iPageIndex + 1);
                bUpdate |= _self.CreatePages(iPageIndex);
            }
            else {
                var nPage2Create = iPageIndex + 1;
                if (nPage2Create >= _self.GetPageCount())
                    nPage2Create = _self.GetPageCount() - 1;
                if (_self.CreatePages(nPage2Create) == true) {
                    bUpdate = true;
                }
            }
            _iCurPageIndex = iPageIndex;
            _ScrollApi.scrollToElement("#" + _self.GetThumb(iPageIndex).GetThumbContainerID(), true, false, OffsetY);
            _ScrollApi.scrollToX(OffsetX);
            if (_self.ProgressiveUpdateState(iPageIndex, _iCurPageIndex, _iCurPageIndex, OffsetY, true, true)) {
                _self.CountDisplayPageRange();
            }

            _self.CreateNearPage(iPageIndex, OffsetY);
            var newVisableRange = _self.GetDisplayPageRange();
            _iCurPageIndex = newVisableRange.current;
            _UpdatePageIndex();
            _self.LoadPages(newVisableRange);
            //恢复监控滚动事件
            _self.BindScrollEvent();

            return true;
        };

        //跳转到下一页
        this.GoNextPage = function () {
            if (_iCurPageIndex >= _self.GetPageCount())
                return;
            _self.GotoPage(_iCurPageIndex + 1);
        };
        //跳转到上一页
        this.GoPrevPage = function () {
            if (_iCurPageIndex <= 0)
                return;
            _self.GotoPage(_iCurPageIndex - 1);
        };


        this.OnSize = function (nNewWidth, nNewHeight) {
            _self.SetThumbsViewWidth(nNewWidth);
            _self.SetThumbsViewHeight(nNewHeight);
            _self.UnBindScrollEvent();
            $("#" + _ThumbsViewContainerID).css({ width: nNewWidth, height: nNewHeight });
            var nOldPage = _iCurPageIndex;
            _self.ReInitializeScrollBar();
            _self.OnPageScroll();
            _self.BindScrollEvent();
            //保持原来的页数;
            _ScrollApi.scrollToElement("#" + _self.GetThumb(nOldPage).GetThumbContainerID(), true, false);
        };

        //设置当前拥有焦点的页面；
        this.SetSelectedPage = function (iPageIndex) {
            if (iPageIndex < 0 || iPageIndex >= _pPDFDoc.GetPageCount())
                return;
            if (_CurSelectedPage == iPageIndex)
                return;
            if (_CurSelectedPage != -1) {
                var pThumb = _self.GetThumb(_CurSelectedPage);
				if (FXREADER.Environment.mobile || FXREADER.Environment.iOS) {
            		$("#" + pThumb.GetThumbContainerID()).removeClass("fwr_thumb_image_selected_wap");
                }
				else
					$("#" + pThumb.GetThumbContainerID()).removeClass("fwr_thumb_image_selected");
                var bPageLoadErr = pThumb.IsLoadError();
                if (bPageLoadErr) {
                    var jqWrap = $("#" + pThumb.GetWrapThumbID());
                    jqWrap.css({ "margin-left": -jqWrap.outerWidth() / 2 });
                }
                else {
                    var jqThumbBGImg = $("#" + pThumb.GetPageBGImgID());
                    jqThumbBGImg.css({ "margin-left": -jqThumbBGImg.outerWidth() / 2 });
                }

            }
            var pNewThumb = _self.GetThumb(iPageIndex);
            var bPageLoadErr = pNewThumb.IsLoadError();
            if (bPageLoadErr) {
                var jqWrap = $("#" + pNewThumb.GetWrapThumbID());
                jqWrap.css({ "margin-left": -jqWrap.outerWidth() / 2 });
            }
            else {
                var jqNewThumbBGImg = $("#" + pNewThumb.GetPageBGImgID());
                jqNewThumbBGImg.css({ "margin-left": -jqNewThumbBGImg.outerWidth() / 2 });
            }
			if (FXREADER.Environment.mobile || FXREADER.Environment.iOS) {
            		 $("#" + pNewThumb.GetThumbContainerID()).addClass("fwr_thumb_image_selected_wap");
                }
				else
					 $("#" + pNewThumb.GetThumbContainerID()).addClass("fwr_thumb_image_selected");
            _CurSelectedPage = iPageIndex;
        };

        //设置当前处于选中状态的页面；
        this.SetThumbFocus = function (iPageIndex) {
            if (iPageIndex < 0 || iPageIndex >= _pPDFDoc.GetPageCount())
                return;
            if (_nThumbFocusPage == iPageIndex)
                return;

            if (_nThumbFocusPage != -1) {
                var pThumb = _self.GetThumb(_nThumbFocusPage);
                var bPageLoadErr = pThumb.IsLoadError();
                if (bPageLoadErr) {
                    var jqWrap = $("#" + pThumb.GetWrapThumbID());
                    jqWrap.removeClass("fwr_thumb_image_emphasized");
                    var out_width = jqWrap.outerWidth();
                    if (out_width > 0)
                        jqWrap.css({ "margin-left": -out_width / 2 });
                }
                else {
                    var jqThumbBGImg = $("#" + pThumb.GetPageBGImgID());
                    jqThumbBGImg.removeClass("fwr_thumb_image_emphasized");
                    jqThumbBGImg.addClass("fwr_thumb_image");
                    var out_width = jqThumbBGImg.outerWidth();
                    if (out_width > 0)
                        jqThumbBGImg.css({ "margin-left": -out_width / 2 });
                }

                $("#" + pThumb.GetThumbPageNumberID()).removeClass("fwr_page_number_focus");

            }
            _self.CountDisplayPageRange();
            if (!_IsPageInVisableRange(iPageIndex)) {
                _self.GotoPage(iPageIndex, 0, 0);
                _self.CreateNearPage(iPageIndex, 0);
            }
            var pNewThumb = _self.GetThumb(iPageIndex);
            var bPageLoadErr = pNewThumb.IsLoadError();
            if (bPageLoadErr) {
                var jqWrap = $("#" + pNewThumb.GetWrapThumbID());
                jqWrap.addClass("fwr_thumb_image_emphasized");
            }
            else {
                var jqNewThumbBGImg = $("#" + pNewThumb.GetPageBGImgID());
                jqNewThumbBGImg.addClass("fwr_thumb_image_emphasized");
                jqNewThumbBGImg.removeClass("fwr_thumb_image");
            }


            $("#" + pNewThumb.GetThumbPageNumberID()).addClass("fwr_page_number_focus");
            _nThumbFocusPage = iPageIndex;
        };

    };

    return FXREADER.PDFView.CPDF_PageThumbsView;

});